package com.fms.fmsapi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fms.fmsapi.entity.Check;
import com.fms.fmsapi.entity.R;
import com.fms.fmsapi.entity.User;
import com.fms.fmsapi.entity.result.CheckResult;
import com.fms.fmsapi.service.UserService;
import com.fms.fmsapi.service.impl.CheckServiceImpl;
import com.fms.fmsapi.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张力文
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/check")
public class CheckController {

    @Autowired
    CheckServiceImpl checkService;

    @Autowired
    UserService userService;

    /**
     * 判断用户是否已经打卡
     * @param token
     * @return
     */
    @GetMapping("/getCheckInfo")
    public R isChecked(String token) {
        String userId = JwtUtil.getUserId(token);
        User user = userService.getById(userId);
        CheckResult checkResult = new CheckResult();
        LambdaQueryWrapper<Check> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Check::getUserUuid, userId)
                        .eq(Check::getStartTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT));
        List<Check> list = checkService.list(lqw);
        Check check = list.get(0);
        BeanUtils.copyProperties(check, checkResult);
        checkResult.setUserName(user.getNickName());
        checkResult.setIsChecked(check.getStatus()==1);
        return R.success(checkResult);
    }

   @PutMapping("/makeCheck")
   public R makeCheck (@RequestBody Check check){
        if(check.getImg().equals("")||check.getImg()==null){
            return R.fail(599,"请上传打卡图片");
        }
        check.setStatus(1);
       boolean flag = checkService.updateById(check);
       if(flag){
           return R.fail(200,"打卡成功");
       } else {
           return R.fail(598,"打卡失败");
       }
   }

   @GetMapping("/getAllCheckInfo")
    public R getAllCheckInfo(String nowDate){

       // 将 "yyyy-MM-dd" 格式的日期字符串转换为 Java 中的 LocalDateTime 对象
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
       LocalDate date = LocalDate.parse(nowDate, formatter);
       LocalDateTime localDateTime = date.atStartOfDay();
        LambdaQueryWrapper<Check> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Check::getStartTime, localDateTime);
        List<Check> list = checkService.list(lqw);
        List<CheckResult> results = new ArrayList<>();
        for(Check check : list){
            CheckResult checkResult = new CheckResult();
            BeanUtils.copyProperties(check, checkResult);
            User user = userService.getById(check.getUserUuid());
            checkResult.setUserName(user!=null?user.getNickName():"该用户已被删除");
            checkResult.setIsChecked(checkResult.getStatus() == 1);
            results.add(checkResult);
        }
        return R.success(results);
    }

    @PutMapping("/makeBack")
    public R makeBack(@RequestBody Check check){
        check.setStatus(3);//被打回
        boolean flag = checkService.updateById(check);
        if(flag){
            return R.fail(200,"打回成功");
        } else {
            return R.fail(598,"打回失败");
        }
    }
}


